﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using VietPay.KooBoo.Data.Infrastructure;
using VietPay.KooBoo.Business.Models;
using VietPay.KooBoo.Business.Interfaces;
using VietPay.KooBoo.Data.Interfaces;
using VietPay.KooBoo.Data;
using VietPay.KooBoo.Data.EntityModels;

namespace VietPay.KooBoo.Business
{
    public class DistributorService : ServiceBase<IDistributorModel>, IDistributorService
    {
        public DistributorService()
            : this(new DistributorRepository())
        {
        }

        private IDistributorRepository _repository;
        public DistributorService(IDistributorRepository newsRepository)
        {
            _repository = newsRepository ?? new DistributorRepository();
        }

        protected override Type LogPrefix
        {
            get { return this.GetType(); }
        }

        public DistributorListViewModel GetDistributorList(int PageSize, int CurrentPage)
        {
            try
            {
                if ((CurrentPage == 0) || (PageSize == 0))
                    return null;

                IQueryable<Distributor> query = _repository.GetQueryable();
                DistributorListViewModel model = new DistributorListViewModel();
                //model.TotalItemCount = query.Count();
                //if (model.TotalItemCount == 0)
                //    return null;
                //model.TotalPageCount = (int)Math.Ceiling((double)model.TotalItemCount / (double)PageSize);
                //if (model.TotalPageCount != 1)
                //    query = query.OrderBy(x => x.DocumentName).Skip((CurrentPage - 1) * PageSize).Take(PageSize);

                //model.ThisPageItemCount = query.Count();

                model.DistributorList = new List<DistributorViewModel>();
                AutoMapper.Mapper.CreateMap<Distributor, DistributorViewModel>()
                    .ForMember(dest => dest.DistributorID, opt => opt.MapFrom(src => src.DistributorID))
                    .ForMember(dest => dest.DistributorCode, opt => opt.MapFrom(src => src.DistributorCode))
                    .ForMember(dest => dest.DistributorName, opt => opt.MapFrom(src => src.DistributorName))
                    .ForMember(dest => dest.Address, opt => opt.MapFrom(src => src.Address))
                    .ForMember(dest => dest.Mobile, opt => opt.MapFrom(src => src.Mobile))
                    .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));
                model.DistributorList = AutoMapper.Mapper.Map(query.ToList(), model.DistributorList);

                return model;
            }
            catch (System.Exception e)
            {
                this.LogError("Error getting the Distributor list", e);
                return null;
            }
        }

        public Distributor ValidateDistributor(string strDistributorCode, string strPassword)
        {
            IQueryable<Distributor> query = _repository.GetQueryable();
            List<Distributor> lstDist= query.Where(p => p.DistributorCode == strDistributorCode).ToList();
            if (lstDist==null || lstDist.Count==0)
            {
                return null;
            }
            return lstDist.FirstOrDefault();
        }

        public DistributorViewModel GetSingle(Expression<Func<DistributorViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public void Add(DistributorViewModel vModel)
        {
            Distributor entity=new Distributor();

            entity.DistributorCode = vModel.DistributorCode;
            entity.DistributorName= vModel.DistributorName;
            entity.Address= vModel.Address;
            entity.Mobile= vModel.Mobile;
            entity.Description= vModel.Description;
            _repository.Add(entity);
        }

        public void Delete(DistributorViewModel vModel)
        {
            Distributor news = _repository.GetAll(p => p.DistributorID== vModel.DistributorID).FirstOrDefault();
            _repository.Delete(news);
        }

        public void Update(DistributorViewModel vModel)
        {
            Distributor entity = _repository.GetAll(p => p.DistributorID == vModel.DistributorID).FirstOrDefault();

            entity.DistributorCode = vModel.DistributorCode;
            entity.DistributorName = vModel.DistributorName;
            entity.Address = vModel.Address;
            entity.Mobile = vModel.Mobile;
            entity.Description = vModel.Description;
            _repository.Attach(entity);
        }

        public IList<DistributorViewModel> GetAll(Expression<Func<DistributorViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public IList<DistributorViewModel> GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<DistributorViewModel> Query(Expression<Func<DistributorViewModel, bool>> whereCondition)
        {
            List<DistributorViewModel> newsQuery = new List<DistributorViewModel>();
            IQueryable<Distributor> query = _repository.GetQueryable();
            AutoMapper.Mapper.CreateMap<Distributor, DistributorViewModel>()
                .ForMember(dest => dest.DistributorID, opt => opt.MapFrom(src => src.DistributorID))
                    .ForMember(dest => dest.DistributorCode, opt => opt.MapFrom(src => src.DistributorCode))
                    .ForMember(dest => dest.DistributorName, opt => opt.MapFrom(src => src.DistributorName))
                    .ForMember(dest => dest.Address, opt => opt.MapFrom(src => src.Address))
                    .ForMember(dest => dest.Mobile, opt => opt.MapFrom(src => src.Mobile))
                    .ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Description));
            newsQuery = AutoMapper.Mapper.Map(query, newsQuery);
            return newsQuery.AsQueryable().Where(whereCondition);
        }

        public long Count(Expression<Func<DistributorViewModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count()
        {
            throw new NotImplementedException();
        }
    }
}
